package com.ruoyi.project.system.trick.controller;

import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.system.equipment.domain.Equipment;
import com.ruoyi.project.system.equipment.service.IEquipmentService;
import com.ruoyi.project.system.list.domain.TrickList;
import com.ruoyi.project.system.list.service.ITrickListService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.trick.domain.VerbalTrick;
import com.ruoyi.project.system.trick.service.IVerbalTrickService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 话术Controller
 * 
 * @author ruoyi
 * @date 2022-02-19
 */
@Controller
@RequestMapping("/system/trick")
public class VerbalTrickController extends BaseController
{
    private String prefix = "system/trick";

    @Autowired
    private IVerbalTrickService verbalTrickService;
    @Autowired
    private IEquipmentService iEquipmentService;
    @Autowired
    private ITrickListService trickListService;

//    @RequiresPermissions("system:trick:view")
    @GetMapping()
    public String trick()
    {
        return prefix + "/trick";
    }

    /**
     * 查询话术列表
     */
//    @RequiresPermissions("system:trick:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VerbalTrick verbalTrick)
    {
        startPage();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        if (!loginUser.equals("admin")){
            verbalTrick.setUserName(loginUser);
        }
        List<VerbalTrick> list = verbalTrickService.selectVerbalTrickList(verbalTrick);
        return getDataTable(list);
    }

    //前端获取话术,请求次数和间隔时间
    @RequestMapping("/selectVerbalTrick")
    @ResponseBody
    public VerbalTrick selectVerbalTrick(Long equipmentId,String userName){
        VerbalTrick verbalTrick = verbalTrickService.selectByEquipmentIdAndUserName(equipmentId,userName);
        return verbalTrick;
    }

    /**
     * 导出话术列表
     */
//    @RequiresPermissions("system:trick:export")
    @Log(title = "话术", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VerbalTrick verbalTrick)
    {
        List<VerbalTrick> list = verbalTrickService.selectVerbalTrickList(verbalTrick);
        ExcelUtil<VerbalTrick> util = new ExcelUtil<VerbalTrick>(VerbalTrick.class);
        return util.exportExcel(list, "话术数据");
    }


    /**
     * 新增话术
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 新增保存话术
     */
//    @RequiresPermissions("system:trick:add")
    @Log(title = "话术", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VerbalTrick verbalTrick)
    {
        String userName = verbalTrick.getUserName();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        //获取设备id
        Long equipmentId = verbalTrick.getEquipmentId();
        //获取输入的话术
        String verbalTrick2 = verbalTrick.getVerbalTrick();
        if (verbalTrick2.isEmpty()){
            return AjaxResult.error("话术不能为空!");
        }
        if (equipmentId==null){
            return AjaxResult.error("设备序号不能为空!");
        }
        if (userName.isEmpty()){
            return AjaxResult.error("登录名称不能为空!");
        }

        if (!userName.equals(loginUser)){
            return AjaxResult.error("请输入本人登录名称!");
        }
        List<Equipment> equipmentList = iEquipmentService.selectEquipmentByEquipmentIdAndUserName(equipmentId, userName);
        if (equipmentList.isEmpty()){
            return AjaxResult.error("该设备不存在请添加设备！");
        }
        //根据设备id和用户名在设备表里查询微信号
        VerbalTrick verbalTrick1 = verbalTrickService.selectVerbalTrickByEquipmentIdAndUserName(equipmentId,userName);
        String wechatId = verbalTrick1.getWechatId();
        verbalTrick.setWechatId(wechatId);
        //根据设备id和用户名在话术表里查询
        VerbalTrick verbalTrick3 = verbalTrickService.selectByEquipmentIdAndUserName(equipmentId, userName);
        if (verbalTrick3!=null){
            return AjaxResult.error("该设备已存在请去修改话术即可!");
//            return AjaxResult.error("请输入本人登录名称!");
        }else{
            return toAjax(verbalTrickService.insertVerbalTrick(verbalTrick));
        }
//       if (userName.equals(loginUser)){
//            return toAjax(verbalTrickService.insertVerbalTrick(verbalTrick));
//        } else {
////           return AjaxResult.error("该设备已存在请去修改话术即可!");
//           return AjaxResult.error("请输入本人登录名称!");
//        }
    }


    /**
     * 修改话术
     */
//    @RequiresPermissions("system:trick:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        VerbalTrick verbalTrick = verbalTrickService.selectVerbalTrickById(id);
        mmap.put("verbalTrick", verbalTrick);
        return prefix + "/edit";
    }

    /**
     * 修改保存话术
     */
//    @RequiresPermissions("system:trick:edit")
    @Log(title = "话术", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VerbalTrick verbalTrick)
    {
        Long id = verbalTrick.getId();
        //获取输入的用户名
        String userName = verbalTrick.getUserName();
        // 获取当前的用户信息
        User currentUser = ShiroUtils.getSysUser();
        // 获取当前的用户名称
        String loginUser = currentUser.getLoginName();
        //获取设备id
        Long equipmentId = verbalTrick.getEquipmentId();

        if (!userName.equals(loginUser)){
            return AjaxResult.error("请不要修改登录名称!");
        }

        List<Equipment> equipmentList = iEquipmentService.selectEquipmentByEquipmentIdAndUserName(equipmentId, userName);
        if (equipmentList.isEmpty()){
            return AjaxResult.error("该设备不存在！");
        }
        //根据设备id和用户名查询微信号
        VerbalTrick verbalTrick1 = verbalTrickService.selectVerbalTrickByEquipmentIdAndUserName(equipmentId, userName);
        String wechatId = verbalTrick1.getWechatId();
        verbalTrick.setWechatId(wechatId);

        VerbalTrick verbalTrick3 = verbalTrickService.selectVerbalTrickById(id);
        Long equipmentIdById = verbalTrick3.getEquipmentId();
        if (!equipmentIdById.equals(equipmentId)){
            return AjaxResult.error("请不要修改设备序号!");
        }
        else {
            return toAjax(verbalTrickService.updateVerbalTrick(verbalTrick));
        }
    }

    /**
     * 删除话术
     */
//    @RequiresPermissions("system:trick:remove")
    @Log(title = "话术", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(verbalTrickService.deleteVerbalTrickByIds(ids));
    }
}
